package com.maila.simplerecord.data;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;

import java.util.List;

/**
 * Created by Administrator on 2017/5/26.
 */
@Table(name = "ProjectProgressdata")
public class ProjectProgressdata extends Model {
    //项目id
    @Column(name = "pid")
    public int pid;
    @Column(name = "subid")
    public int subid;
    //内容
    @Column(name = "subject_content")
    public String subject_content;
    //时间
    @Column(name = "subject_time")
    public String subject_time;
    @Column(name = "subject_note")
    public String subject_note;


    //单条查询
    public static ProjectProgressdata getRandom(int subid) {
        return new Select()
                .from(ProjectProgressdata.class)
                .where("subid = ?", subid)
                .orderBy("RANDOM()")
                .executeSingle();
    }

    //多条查询
    public static List<ProjectProgressdata> getAll(int pid) {
        return new Select()
                .from(ProjectProgressdata.class)
                .where("pid = ?", pid)
                .orderBy("id DESC")
                .execute();
    }

    public static int getlastsubid() {
        if (new Select()
                .from(ProjectProgressdata.class)
                .execute()!=null){
            List<ProjectProgressdata> datas=new Select()
                    .from(ProjectProgressdata.class)
                    .orderBy("subid ASC")
                    .execute();
            if(datas!=null&&datas.size()>1){
                return  datas.get(datas.size()-1).subid;
            }else {
                return  0;
            }

        }else{
            return 0;
        }

    }
    public static void deleteByID(int subid) {
        new Delete().from(ProjectProgressdata.class).where("subid = ?", subid).execute();
    }
    public static void deleteByPID(int pid) {
        if(getAll(pid)!=null&&getAll(pid).size()>0){
            List<ProjectProgressdata> mdata=getAll(pid);
            for(int i=0;i<mdata.size();i++){
                new Delete().from(ProjectProgressdata.class).where("subid = ?", mdata.get(i).subid).execute();
            }
        }


    }

    public static void clear() {
        new Delete().from(ProjectProgressdata.class).execute();
    }


}
